import {
  exchangeCardList,
  replyExchangeCard
} from '../../apis/group'
const app = getApp();

Page({
  data: {
    applyList: [],
    pageNum: 1,
    isHaveMore: false,
    isRequest: false
  },

  onLoad() {
    this.getExchangeList(1, 10);
  },

  onShow() {

  },

  onReachBottom() {
    if (this.data.isHaveMore) {
      this.setData({
        pageNum: this.data.pageNum + 1
      });
      this.getExchangeList(this.data.pageNum, 10);
    };
  },

  getExchangeList(pageNum, pageSize) {
    exchangeCardList(pageNum, pageSize).then(res => {
      if (res.data.code === 200) {
        let {
          rows
        } = res.data;
        rows.forEach(v => {
          if (!new RegExp('https://').test(v.userCard.avatar) && !new RegExp('http://').test(v.userCard.avatar)) v.userCard.avatar = app.globalData.url + v.userCard.avatar;
        })
        this.setData({
          applyList: rows
        });
      };
    });
  },

  action(e) {
    let {
      id,
      type,
      index
    } = e.currentTarget.dataset;
    if (type == 0) { // 拒绝
      this.replyExchange(id, 0, index);
    } else { // 同意
      this.replyExchange(id, 1, index);
    };
  },

  replyExchange(exchangeId, agree, index) {
    replyExchangeCard(exchangeId, agree).then(res => {
      if (res.data.code === 200) {
        if (agree == 1) {
          this.data.applyList[index].reAgree = 1;
        } else if (agree == 0) {
          this.data.applyList[index].reAgree = 0;
        };
        this.setData({
          applyList: this.data.applyList
        });
      };
    });
  }
})